package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateDeviceAuthFileDTO;
import com.itlong.cloud.POJO.PO.DeviceAuthFilePO;
import com.itlong.cloud.POJO.PO.EvidenceFileInfoPO;
import com.itlong.cloud.POJO.VO.operate.DeviceAuthFileVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperateDeviceWhiteListAuthFileDao;
import com.itlong.cloud.operate.service.IOperateDeviceWhiteListAuthFileService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      运营-设备白名单授权文件管理服务层接口实现类
 * </desc>
 *
 * @createDate 2020/8/13
 **/
@Service
public class OperateDeviceWhiteListAuthFileServiceImpl implements IOperateDeviceWhiteListAuthFileService {

    @Autowired
    private IOperateDeviceWhiteListAuthFileDao iOperateDeviceWhiteListAuthFileDao;
    /**
     * <desc>
     *      分页获取授权文件列表
     * </desc>
     *
     * @param operateDeviceAuthFileDTO  授权文件参数DTO
     * @return 授权文件分页列表
     * @Author Zhu.yj
     * @createDate  2020/8/14
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<DeviceAuthFileVO> getPage(OperateDeviceAuthFileDTO operateDeviceAuthFileDTO) throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateDeviceAuthFileDTO);
        Page<DeviceAuthFileVO> page = new Page<>();
        page.getPage().setCurrentPage(operateDeviceAuthFileDTO.getCurrentPage());
        page.getPage().setPageSize(operateDeviceAuthFileDTO.getPageSize());
        params.put("startSlip", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        page.setRows(iOperateDeviceWhiteListAuthFileDao.getPage(params), iOperateDeviceWhiteListAuthFileDao.getPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      保存授权文件信息
     * </desc>
     *
     * @param operateDeviceAuthFileDTO 授权文件参数DTO
     * @return 受影响行数
     * @Author Zhu.yj
     * @createDate  2020/8/14
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer save(OperateDeviceAuthFileDTO operateDeviceAuthFileDTO) throws Exception {
        DeviceAuthFilePO deviceAuthFilePO = new DeviceAuthFilePO();
        Date nowTime = new Date();
        BeanUtils.copyProperties(operateDeviceAuthFileDTO, deviceAuthFilePO);
        deviceAuthFilePO.setAuthId(LogicIdUtil.bussinessId());
        //创建时已使用次数为0
        deviceAuthFilePO.setEffectiveTimes(Integer.valueOf(operateDeviceAuthFileDTO.getEffectiveTimes()));
        deviceAuthFilePO.setUsedTimes(0);
        deviceAuthFilePO.setCreateUserId(operateDeviceAuthFileDTO.getLoginUserId());
        deviceAuthFilePO.setOperateUserId(operateDeviceAuthFileDTO.getLoginUserId());
        deviceAuthFilePO.setStatus(DataStatusEnum.NORMAL.getType());
        deviceAuthFilePO.setCreateTime(nowTime);
        deviceAuthFilePO.setUpdateTime(nowTime);
        Map<String, Object> params = SqlUtil.durableData(deviceAuthFilePO, PlatformConstants.TABLE_SAVE);
        iOperateDeviceWhiteListAuthFileDao.save(params);
        //保存次数凭证文件信息
        if (operateDeviceAuthFileDTO.getEvidenceFileUrlList() != null && !operateDeviceAuthFileDTO.getEvidenceFileUrlList().isEmpty()){
            List<EvidenceFileInfoPO> fileInfoPOS = new ArrayList<>();
            for (Map<String, Object> evidenceMap : operateDeviceAuthFileDTO.getEvidenceFileUrlList()){
                EvidenceFileInfoPO evidenceFileInfoPO = new EvidenceFileInfoPO();
                evidenceFileInfoPO.setFileId(LogicIdUtil.bussinessId());
                evidenceFileInfoPO.setAuthId(deviceAuthFilePO.getAuthId());
                evidenceFileInfoPO.setFileUrl(evidenceMap.get("fileUrl") + "");
                evidenceFileInfoPO.setFileName(evidenceMap.get("fileName") + "");
                evidenceFileInfoPO.setCreateUserId(operateDeviceAuthFileDTO.getLoginUserId());
                evidenceFileInfoPO.setOperateUserId(operateDeviceAuthFileDTO.getLoginUserId());
                evidenceFileInfoPO.setStatus(DataStatusEnum.NORMAL.getType());
                evidenceFileInfoPO.setCreateTime(nowTime);
                evidenceFileInfoPO.setUpdateTime(nowTime);
                fileInfoPOS.add(evidenceFileInfoPO);
            }
            iOperateDeviceWhiteListAuthFileDao.batchSaveEvidenceFile(fileInfoPOS);
        }
        return 1;
    }

    /**
     * <desc>
     *      删除授权文件信息
     * </desc>
     *
     * @param operateDeviceAuthFileDTO 授权文件参数DTO
     * @return 首影响行数
     * @Author Zhu.yj
     * @createDate  2020/8/14
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer delete(OperateDeviceAuthFileDTO operateDeviceAuthFileDTO) throws Exception {
        iOperateDeviceWhiteListAuthFileDao.deleteEvidenceFile(operateDeviceAuthFileDTO.getAuthId());
        return iOperateDeviceWhiteListAuthFileDao.delete(operateDeviceAuthFileDTO.getAuthId());
    }

    /**
     * <desc>
     * 获取授权文件信息
     * </desc>
     *
     * @param operateDeviceAuthFileDTO
     * @Author chengshican
     * @Date 2020-08-15 10:44
     * @Param arams 查询参数map
     * @Return 授权文件信息
     * @Exception
     */
    @Override
    public DeviceAuthFileVO getEffectiveAuthFile(OperateDeviceAuthFileDTO operateDeviceAuthFileDTO) throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateDeviceAuthFileDTO);
        return iOperateDeviceWhiteListAuthFileDao.getEffectiveAuthFile(params);
    }

    /**
     * <desc>
     * 根据授权文件id更新可用次数和已使用次数
     * </desc>
     *
     * @param operateDeviceAuthFileDTO
     * @Author chengshican
     * @Date 2020-08-16 9:03
     * @Param 参数请求对象 operateDeviceAuthFileDTO
     * @Return
     * @Exception
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED,rollbackFor = Exception.class)
    @Override
    public int updateAuthFileByAuthId(OperateDeviceAuthFileDTO operateDeviceAuthFileDTO) throws Exception {
        return iOperateDeviceWhiteListAuthFileDao.updateAuthFileByAuthId(operateDeviceAuthFileDTO.getAuthId(), operateDeviceAuthFileDTO.getEffectiveTimes(), operateDeviceAuthFileDTO.getUsedTimes());
    }

    /**
     * <desc>
     *   获取次数凭证地址
     * </desc>
     *
     * @param authId 授权文件id
     * @return 包含key为 fileName, fileUrl的map的集合
     * @Author Zhu.yj
     * @createDate  2020/8/16
     */
    @Override
    public List<Map<String, Object>> getTimesEvidence(String authId) throws Exception {
        return iOperateDeviceWhiteListAuthFileDao.getTimesEvidence(authId);
    }

    /**
     * <desc>
     *      根据文件名称获取文件数量
     * </desc>
     *
     * @param fileName 文件名称
     * @return 文件名为fileName的数量
     * @Author Zhu.yj
     * @createDate  2020/8/17
     */
    @Override
    public Integer getCountByFileName(String fileName) throws Exception {
        return iOperateDeviceWhiteListAuthFileDao.getCountByFileName(fileName);
    }
}
